package com.pay.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pay.common.base.BaseService;
import com.pay.mapper.auto.PayAccountMapper;
import com.pay.model.auto.PayAccount;
import com.pay.model.auto.PayAccountExample;
import com.pay.model.custom.Tablepar;
import com.pay.util.SnowflakeIdWorker;
import com.pay.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 支付账户Service
 *
 * @author fuce
 * @Title: PayAccountService.java 
 * @Package com.pay.service 
 * @email 87766867@qq.com
 * @date 2019-06-24 10:54:26  
 */
@Service
public class PayAccountService implements BaseService<PayAccount, PayAccountExample> {
    @Autowired
    private PayAccountMapper payAccountMapper;

    /**
     * 分页查询
     *
     * @param tablepar
     * @return
     */
    public PageInfo<PayAccount> list(Tablepar tablepar) {
        PayAccountExample testExample = new PayAccountExample();
        testExample.setOrderByClause("id ASC");

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<PayAccount> list = payAccountMapper.selectByExample(testExample);
        PageInfo<PayAccount> pageInfo = new PageInfo<PayAccount>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return payAccountMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(PayAccount record) {
        if(StringUtils.isEmpty(record.getPayUrl())){
            return 0;
        }
        // 序号+1
        PayAccountExample payAccountExample = new PayAccountExample();
        payAccountExample.createCriteria().andPayTypeEqualTo(record.getPayType());
        long count = payAccountMapper.countByExample(payAccountExample);
        record.setPayNumber(Long.valueOf(count).intValue() + 1);

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());
        record.setCreateDate(new Date());
        return payAccountMapper.insertSelective(record);
    }

    @Override
    public PayAccount selectByPrimaryKey(String id) {
        return payAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PayAccount record) {
        return payAccountMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByExampleSelective(PayAccount record, PayAccountExample example) {

        return payAccountMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(PayAccount record, PayAccountExample example) {

        return payAccountMapper.updateByExample(record, example);
    }

    @Override
    public List<PayAccount> selectByExample(PayAccountExample example) {

        return payAccountMapper.selectByExample(example);
    }

    @Override
    public long countByExample(PayAccountExample example) {

        return payAccountMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(PayAccountExample example) {

        return payAccountMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param payAccount
     * @return
     */
    public int checkNameUnique(PayAccount payAccount) {
        PayAccountExample example = new PayAccountExample();
        List<PayAccount> list = payAccountMapper.selectByExample(example);
        return list.size();
    }
}
